package cn.tsmchina.one.service.category;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tsmchina.one.dao.category.CategoryDao;
import cn.tsmchina.one.dao.product.ProductDao;
import cn.tsmchina.one.domain.category.Category;
import cn.tsmchina.one.domain.product.Product;

@Service
public class CategoryServiceImpl implements CategoryService
{
	@Autowired
	private CategoryDao categoryDao;
	
	@Autowired
	private ProductDao productDao;

	@Override
	public List<Category> getAllSubRestaurant(long customerId) {
		return categoryDao.getOneLevelCategories(customerId);
	}

	@Override
	public List<Category> getAllFoodCategory(String subRestaurant, long customerId) {
		return categoryDao.getChildrenByParent(subRestaurant, customerId);
	}

	@Override
	public Category getById(int id) {
		return categoryDao.getById(id);
	}

	@Override
	public List<Category> getSubRestaurant(int page, long customerId) {
		return categoryDao.getOneLevelCategories(page, customerId);
	}

	@Override
	public void updateCategory(Category category) {
		categoryDao.updateCategory(category);
	}

	@Override
	public void deleteCategory(int id) {
		Category delCategory = getById(id);
		//one step: delete sub category
		List<Category> sub = getAllFoodCategory(delCategory.getCid(), delCategory.getCustomerId());
		if(sub.size()!=0) {
			for (Category category : sub) {
				categoryDao.deleteCategory(category.getId());
			}
		}
		
		//two step: delete products below sub category
		for (Category category : sub) {
			List<Product> deleteProducts = productDao.getAllFoodsByCategory(category.getId());
			for (Product product : deleteProducts) {
				productDao.deleteById(product.getId());
			}
		}
		
		//three step: delete products below this sub category
		List<Product> deleteProducts = productDao.getAllFoodsByCategory(delCategory.getId());
		for (Product product : deleteProducts) {
			productDao.deleteById(product.getId());
		}
		
		//three step: delete this
		categoryDao.deleteCategory(id);
	}

	@Override
	public void addCategory(Category category) {
		categoryDao.addCategory(category);
	}

	@Override
	public String genId(String pid, long customerId) {
		List<Category> categories = null;
		List<String> cids = new ArrayList<>();
		if(pid==null) {
			categories = categoryDao.getOneLevelCategories(customerId);
			for (Category category : categories) {
				cids.add(category.getCid());
			}
		} else {
			categories = categoryDao.getChildrenByParent(pid, customerId);
			for (Category category : categories) {
				cids.add(category.getCid().substring(pid.length()+1));
			}
		}
		return findMax(pid, cids);
	}

	private String findMax(String pid, List<String> cids) {
		int maxNum = -1;
		for (String string : cids) {
			for(int i=0; i<string.length(); i++) {
				if(string.charAt(i)!='0' || i==string.length()-1) {
					String tmp = string.substring(i);
					int numTmp = Integer.parseInt(tmp);
					if(numTmp>maxNum)
						maxNum=numTmp;
				}
			}
		}
		int nextNum = maxNum+1;
		String categorySubStr = null;
		if(nextNum<10) {
			categorySubStr = "000"+nextNum;
		} else if(nextNum<100) {
			categorySubStr = "00"+nextNum;
		} else if(nextNum<1000) {
			categorySubStr = "0"+nextNum;
		} else {
			categorySubStr = ""+nextNum;
		}
		if(pid==null)
			return categorySubStr;
		else
			return pid + "-" + categorySubStr;
	}

	@Override
	public Category getByCategoryCid(String categoryCid, long customerId) {
		return categoryDao.getByCategoryCid(categoryCid, customerId);
	}

	@Override
	public Category getByCategoryLoginId(Integer loginId) {
		// TODO Auto-generated method stub
		return categoryDao.getByCategoryLoginId(loginId);
	}

	@Override
	public String getNameByLoginId(Integer loginId) {
		// TODO Auto-generated method stub
		return categoryDao.getNameByLoginId(loginId);
	}
	

}
